#include "downloadHeaders/downloadviewh/historyitemwidget.h"
#include "ui_historyitemwidget.h"
#include "downloadHeaders/downloadmodelh/historyitemmodel.h"
#include <QPushButton>
#include <QMetaEnum>
#include <QDesktopServices>
#include <QGraphicsDropShadowEffect>
#include <QProcess>
historyitemwidget::historyitemwidget(QWidget *parent, QString file, QString dir, QString url, QString state, QString time):
    QWidget(parent),
    ui(new Ui::historyitemwidget)
{
    ui->setupUi(this);
//打开文件夹
    ui->folder->setVisible(false);
    ui->folder->setStyleSheet("color:blue;");
//对model有用的属性初始化
    model = new historyitemmodel();
    this->model->downloadFileName = file;
    this->model->downloadFileDir = dir;
//图标
    QIcon icon = model->getIconByFileEx();
    QPixmap m_pic = icon.pixmap(icon.actualSize(QSize(41, 41)));//size自行调整
    ui->icon->setPixmap(m_pic);
//根据状态分别显示
    if(state == QString::fromLocal8Bit("DownloadCompleted"))
    {
        model->currentState = model->DownloadCompleted;
        setStyleSheet("background-color: rgb(244,244,244);");
        ui->folder->setVisible(true);
        ui->state->setVisible(false);
    }
    else if(state == QString::fromLocal8Bit("DownloadCancelled"))
    {
        model->currentState = model->DownloadCancelled;
        setTitleStyle();
        setStyleSheet("background-color: rgb(200,200,200);");
    }
    else
    {
        model->currentState = model->DownlaodDeleted;
        setStyleSheet("background-color: rgb(200,200,200);");
        setTitleStyle();
    }
//设置文本信息
    ui->title->setText(file);
    ui->url->setText(QString::fromLocal8Bit("<a style='color: blue;' href = %1> %2 </a>").arg(url, url));
//    ui->url->setText(url);
    ui->state->setText(state);
    ui->time->setText(time);
    setShadowEffect(this);
    ui->deleterecord->setIcon(QIcon(":/resource/imgs/cancel.png"));
    ui->deleterecord->setToolTip("delete from local database");

    connect(ui->deleterecord, &QPushButton::clicked, this->model, &historyitemmodel::deleteRecord);
    connect(ui->deleterecord, &QPushButton::clicked, this, &historyitemwidget::emitDeleteClicked);
    connect(ui->folder, &QPushButton::clicked, this, &historyitemwidget::openfolder);

}
void historyitemwidget::setShadowEffect(QWidget * w)
{
    QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect(w);
    effect->setOffset(4, 4);//设置阴影距离
    effect->setColor(QColor(0,0,0,90));//设置阴影颜色
    effect->setBlurRadius(10);//设置阴影圆角
    w->setGraphicsEffect(effect);
}
void historyitemwidget::setTitleStyle()
{
    ui->title->setStyleSheet("text-decoration:line-through;");
}

void historyitemwidget::emitDeleteClicked()
{
    emit clicked(this);
}
void historyitemwidget::openfolder()
{
    //QDesktopServices::openUrl(QUrl(model->downloadFileDir));
    QProcess process;
    QString filePath = model->downloadFileDir + model->downloadFileName;
    filePath.replace("/", "\\"); // 只能识别 "\"
    QString cmd = QString("explorer.exe /select,\"%1\"").arg(filePath);
    qDebug() << cmd;
    process.startDetached(cmd);
}
historyitemwidget::~historyitemwidget()
{
    delete ui;
}
